package com.tobby.camel.camelinaction.chapter5.onexception;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.ConnectException;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;

public class OnExceptionGapTest extends CamelTestSupport {

	@Override
	public boolean isUseRouteBuilder() {
		// each unit test case include their own route builder
		return false;
	}

	@Test
	public void testOnExceptionGap() throws Exception {
		context.addRoutes(new RouteBuilder() {

			@Override
			public void configure() throws Exception {
				context.setTracing(true);
				onException(ConnectException.class).maximumRedeliveries(5);
				onException(IOException.class).maximumRedeliveries(3)
						.redeliveryDelay(1000);
				onException(Exception.class).maximumRedeliveries(1).redeliveryDelay(5000);

				from("direct:order").bean(OrderServiceBean.class, "handleOrder")
						.bean(OrderServiceBean.class, "enrichFromFile");
			}
		});
		context.start();
		try {
			template.requestBody("direct:order", "Camel in Action");
			fail();
		}
		catch (Exception e) {
			assertIsInstanceOf(OrderFailedException.class, e.getCause());
			assertIsInstanceOf(FileNotFoundException.class, e.getCause().getCause());
		}
	}

}
